﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace 映客答题
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button2_Click(object sender, EventArgs e)
		{

		}



		public void GeneralBasicDemo(string fName)
		{
			Baidu.Aip.Ocr.Ocr client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
			var image = System.IO.File.ReadAllBytes(fName);
			// 调用通用文字识别, 图片参数为本地图片，可能会抛出网络等异常，请使用try/catch捕获
			var result = client.GeneralBasic(image);
			Console.WriteLine(result);
			// 如果有可选参数
			var options = new Dictionary<string, object>{
		{"language_type", "CHN_ENG"},
		{"detect_direction", "true"},
		{"detect_language", "true"},
		{"probability", "true"}
	};
			// 带参数调用通用文字识别, 图片参数为本地图片
			result = (JObject)client.GeneralBasic(image, options);
			JArray w = (JArray)JsonConvert.DeserializeObject(result["words_result"].ToString());
			string s = "";
			for(int i = 0; i < w.Count; i++)
			{
				s += w[i]["words"].ToString();
			}
			this.richTextBox1.Text = s;

			//Console.WriteLine(result);
		}
		public void GeneralBasicUrlDemo()
		{
			var url = "https//www.x.com/sample.jpg";

			Baidu.Aip.Ocr.Ocr client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
			// 调用通用文字识别, 图片参数为远程url图片，可能会抛出网络等异常，请使用try/catch捕获
			var result = client.GeneralBasicUrl(url);
			Console.WriteLine(result);
			// 如果有可选参数
			var options = new Dictionary<string, object>{
		{"language_type", "CHN_ENG"},
		{"detect_direction", "true"},
		{"detect_language", "true"},
		{"probability", "true"}
	};
			// 带参数调用通用文字识别, 图片参数为远程url图片
			result = client.GeneralBasicUrl(url, options);
			Console.WriteLine(result);
		}


		string APP_ID = "10632123";
		string API_KEY = "9BQg8QLHyaLC06tqyUiQZVSw";
		string SECRET_KEY = "cXZlrYzPt8LKtAh2ETPTNTFQeU6aIDKK";

		
		private void button1_Click(object sender, EventArgs e)
		{
			// 设置APPID/AK/SK
			GeneralBasicDemo(this.textBox1.Text);
		}
	}
}
